package com.qf.utils.http;

import cn.hutool.core.io.FileUtil;
import com.qf.common.configure.HttpClientConfig;
import com.qf.common.configure.ProxyInfo;
import com.qf.common.utils.HttpClientUtil;
import com.qf.common.utils.StringUtil;
import com.qf.utils.SmallTool;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;

public class HttpCall implements Callable<String> {
    private String url;

    public HttpCall(String url) {
        this.url = url;
    }

    public static String getPmCurlHtml(String phpUrl, String url) {
        url = "http://" + phpUrl + "/pm-curl.php?url=" + StringUtil.encodeUrl(url);
        ProxyInfo pi = new ProxyInfo();
//        pi.setTimeout(50000);
        HttpClientConfig config = new HttpClientConfig(pi, url, null, true);
        String html = HttpClientUtil.get(config);
        return html;
    }

    //相关图片用到的暂时不需要替换 写死
    public static String getHtml(String url) {
        ProxyInfo pi = new ProxyInfo();
//        pi.setTimeout(50000);
        HttpClientConfig config = new HttpClientConfig(pi, url, null, true);
        String html = HttpClientUtil.get(config);
//        CloseableHttpClient client = HttpClients.custom().build();;
//        String html = "";
//        try {
//            HttpGet httpGet = new HttpGet(url);
//            RequestConfig requestConfig = RequestConfig.custom()
//                    .setSocketTimeout(50000).setConnectTimeout(50000).setRedirectsEnabled(true).build();
//            httpGet.setConfig(requestConfig);
//            CloseableHttpResponse response = null;
//            HttpEntity entity = null;
//
//            try {
//                response = client.execute(httpGet);
//                entity = response.getEntity();
//                html = EntityUtils.toString(entity, "utf-8");
//            } catch (IOException e) {
//                e.printStackTrace();
//            } finally {
//                try {
//                    EntityUtils.consume(entity);
//                    if (response != null) {
//                        response.close();
//                    }
//
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        } catch (Exception e) {
//
//        }
//        try {
//            client.close();
//        } catch (Exception e) {
//
//        }
        return html;
    }

    public static void main(String[] args) {

        String htmlPost = getHtmlPost("36833438");
        FileUtil.writeUtf8String(htmlPost, "/usr/local/gcr/30535.xml");
        System.out.println(htmlPost);
    }

    //更新 申请全文没有的时候获取数据 用到的
    public static String getHtmlPost(String ids) {
        String url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi";
        List<NameValuePair> list = new LinkedList<>();
        BasicNameValuePair param1 = new BasicNameValuePair("db", "pubmed");
        BasicNameValuePair param2 = new BasicNameValuePair("retmode", "xml");
        BasicNameValuePair param3 = new BasicNameValuePair("id", ids);
        list.add(param1);
        list.add(param2);
        list.add(param3);
        ProxyInfo pi = new ProxyInfo();
//        pi.setTimeout(50000);
        HttpClientConfig config = new HttpClientConfig(pi, url, null, true);
        config.setNvpsList(list);
        String html = HttpClientUtil.post(config);
//        CloseableHttpClient client = HttpClients.custom().build();
//        String html = "";
//        try {
//            HttpPost httpPost = new HttpPost("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi");
//            List<NameValuePair> list = new LinkedList<>();
//            BasicNameValuePair param1 = new BasicNameValuePair("db", "pubmed");
//            BasicNameValuePair param2 = new BasicNameValuePair("retmode", "xml");
//            BasicNameValuePair param3 = new BasicNameValuePair("id", ids);
//            list.add(param1);
//            list.add(param2);
//            list.add(param3);
//            // 使用URL实体转换工具
//            UrlEncodedFormEntity urlEntity = null;
//            try {
//                urlEntity = new UrlEncodedFormEntity(list, "UTF-8");
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }
//            httpPost.setEntity(urlEntity);
//            RequestConfig requestConfig = RequestConfig.custom()
//                    .setSocketTimeout(50000).setConnectTimeout(50000).setRedirectsEnabled(true).build();
//            httpPost.setConfig(requestConfig);
//            CloseableHttpResponse response = null;
//            HttpEntity entity = null;
//            try {
//                response = client.execute(httpPost);
//                entity = response.getEntity();
//                html = EntityUtils.toString(entity, "utf-8");
//            } catch (IOException e) {
//                e.printStackTrace();
//            } finally {
//                try {
//                    EntityUtils.consume(entity);
//                    if (response != null) {
//                        response.close();
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        } catch (Exception e) {
//
//        }
//        try {
//            client.close();
//        } catch (Exception e) {
//        }
        return html;
    }

    public static String downFile(String phpUrl, String url, String referer, String token, List<NameValuePair> list) {
//        String fileUrl = "http://23.228.89.166:808/curl.php?url=" + StringUtil.encodeUrl(url) + "&referer=" + StringUtil.encodeUrl(referer) + "&cookie=" + StringUtil.encodeUrl("labs-pubmed-csrftoken=" + token);
        String fileUrl = "http://" + phpUrl + "/curl.php?url=" + StringUtil.encodeUrl(url) + "&referer=" + StringUtil.encodeUrl(referer) + "&cookie=" + StringUtil.encodeUrl("labs-pubmed-csrftoken=" + token);
        ProxyInfo pi = new ProxyInfo();
        HttpClientConfig config = new HttpClientConfig(pi, fileUrl, "", true, 5600);
        config.setNvpsList(list);
        String html = HttpClientUtil.post(config);
        return html;
    }

    // 多线程获取数据xml 数据 xml 地址可以多线程获取 2023年8月28日17:46:55
    public static String downXmlFile(String phpUrl, String pmid) {
        String url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=xml&id=" + pmid;
        String fileUrl = "http://" + phpUrl + "/curl.php?url=" + StringUtil.encodeUrl(url);
        ProxyInfo pi = new ProxyInfo();
        HttpClientConfig config = new HttpClientConfig(pi, fileUrl, "", true, 56000);
        String html = HttpClientUtil.get(config);
        return html;
    }

    //自己的jar包进行获取数据 自己写的一个额外服务
    public static String downFileNewTomcat(String phpUrl, String format, String ids) {
        String s = phpUrl;
        SmallTool.printMess("随机地址是脚本++址是+" + s);
        String fileUrl = "http://" + s + "/index/export?format=" + format + "&ids=" + ids;
        ProxyInfo pi = new ProxyInfo();
        HttpClientConfig config = new HttpClientConfig(pi, fileUrl, "", true, 56000);
        List<NameValuePair> list = new LinkedList<>();
        config.setNvpsList(list);
        String html = HttpClientUtil.post(config);
        return html;
    }

    /**
     * 导出
     *
     * @param phpUrl php地址
     * @param format 格式
     * @param param  词语
     * @param sort   新加的排序 2022年7月6日10:28:07
     * @param filter 过滤
     * @return
     */
    public static String downFileNewTomcatParam(String phpUrl, String format, String param, String sort, String filter) {
        // 默认走全部服务器随机地址 目前三个服务器
        String s = phpUrl;
        SmallTool.printMess("随机地址是脚本地址是+" + phpUrl);
//        String fileUrl = "http://" + phpUrl + ":12544/index/export-param-param";
        String fileUrl = "http://" + s + "/index/export-param-param";
        SmallTool.printMess("地址是++" + fileUrl);
        ProxyInfo pi = new ProxyInfo();
        HttpClientConfig config = new HttpClientConfig(pi, fileUrl, "", true, 560000);
        List<NameValuePair> list = new LinkedList<>();
        list.add(new BasicNameValuePair("format", format));
        list.add(new BasicNameValuePair("param", URLEncoder.encode(param)));
        list.add(new BasicNameValuePair("filter", filter));
        if (StringUtil.isNotNull(sort)){
            list.add(new BasicNameValuePair("sort", sort));
        }

        config.setNvpsList(list);
        String html = HttpClientUtil.post(config);
        return html;
    }

    //自己的jar包进行获取数据 自己写的一个额外服务
    // http://23.228.89.166:12544/index/export-param?format=pmid&param=hand%20f

    @Override
    public String call() {
        ProxyInfo pi = new ProxyInfo();
        HttpClientConfig config = new HttpClientConfig(pi, url, null, true);
        String html = HttpClientUtil.get(config);
        //84.33.244.237:808|104.233.249.253:8149|104.233.249.253:8845|104.233.249.253:8109
        //最后面那个不能用了
//        CloseableHttpClient client = HttpClients.custom().build();;
//        try {
//            HttpGet httpGet = new HttpGet(url);
//            RequestConfig requestConfig = RequestConfig.custom()
//                    .setSocketTimeout(50000).setConnectTimeout(50000).setRedirectsEnabled(true).build();
//            httpGet.setConfig(requestConfig);
//            CloseableHttpResponse response = null;
//            HttpEntity entity = null;
//            try {
//                response = client.execute(httpGet);
//                entity = response.getEntity();
//                html = EntityUtils.toString(entity, "utf-8");
//            } catch (IOException e) {
//                e.printStackTrace();
//            } finally {
//                try {
//                    EntityUtils.consume(entity);
//                    response.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        } catch (Exception e) {
//
//        } finally {
//            client.close();
//        }
        return html;
    }

//    public static void main(String[] args) {
////        String urls = "http://23.228.89.166:808/pm-details.php?term="+19238210;
////        String urls = "http://23.228.89.166:808/pm-newsearch1.php?term=nee&filter=articleattr.data&format=abstract";
////        String urls = "http://23.228.89.166:808/pm-newsearch.php?term="+StringUtil.encodeUrl("TGF BETA3 ")+"&format=abstract";
////        String urls = "http://23.228.89.165:808/pm-newsearch1.php?term=Chin%20Med%20J";
//        String urls = "http://23.228.89.165:808/pm-newsearch1.php?term=Chin%20Med%20J";
////        String urls = "https://www.ncbi.nlm.nih.gov/mesh/1000069";
//////        String urls = "https://pubmed.ncbi.nlm.nih.gov/?term=foot&filter=articleattr.data&format=abstract";
//        System.out.println(getHtml(urls));
//    }

}


